package com.baizhi.util;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.apache.commons.io.FileUtils;

import javax.servlet.http.HttpSession;

public class FilePicture {
	//文件上传
	public static String upPicture(File file, String fileFileName, HttpSession session) {
		//为解决重名问题
		String imgName = "";
		try {
			//拼接uuid
			String uid = UUID.randomUUID().toString();
			String realPath = session.getServletContext().getRealPath("/user/img");
			//获取绝对路径
			File destFile = new File(realPath + "/" + uid + fileFileName);
			FileUtils.copyFile(file, destFile);
			imgName = uid + fileFileName;
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		//返回一个拼接uuid的图片名
		return imgName;
	}
	
	//文件删除
	public static void deletePicture(String fileName, HttpSession session){
		//获取绝对路径
		String realPath = session.getServletContext().getRealPath("/img");
		File file = new File(realPath + "/" + fileName);
		file.delete();
	}
}
